﻿layui.use(['form', 'layer', 'jquery', 'tree'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var $ = layui.jquery;
    var token = localStorage.getItem("token");
    var tree = layui.tree;
    console.log(token)

    $.ajax({
        url: '../User/RoleListDo',
        type: "get",
        data: { token: token },
        success: function (res) {
            res = JSON.parse(res);
            console.log(res)

            var selectData = res.data;
            var selectHtml = '<option value="">请选择</option>';
            selectData.forEach(function (item) {
                selectHtml += '<option value="' + item.id + '">' + item.name + '</option>';
            });
            $('#dynamicSelectRole').html(selectHtml);
            form.render('select');
            form.on('select(dynamicFilterRole)', function (data) {
                console.log(data.value);
            });
        },
        error: function (err) {
            console.log(err)
        }
    });


    $.ajax({
        url: '../User/MenuListDo',
        type: "get",
        data: { token: token },
        success: function (res) {
            res = JSON.parse(res);
            console.log(res)
            var selectData = res.data.map(item => {
                return {
                    title: item.label,
                    id: item.value,
                    children: item.children ? item.children.map(child => ({
                        title: child.label,
                        id: child.value,
                        children: child.children ? child.children.map(subChild => ({
                            title: subChild.label,
                            id: subChild.value,
                        })) : []
                    })) : []  // 假设初始时没有子节点
                };
            });
            tree.render({
                elem: '#dynamicSelect',
                data: selectData,
                showCheckbox: true,
                id: "add",
                edit: ['add', 'update', 'del'], // 开启节点的右侧操作图标
                oncheck: function (obj) {

                    var menuId = []


                    // 递归函数，用于收集所有选中节点的id
                    function collectIds(checkedNodes) {
                        checkedNodes.forEach(function (node) {
                            menuId.push(node.id); // 将选中节点的id添加到数组
                            // 如果当前节点有子节点，递归调用以收集子节点的id
                            if (node.children) {
                                collectIds(node.children);
                            }
                        });
                    }

                    // 获取所有选中的节点
                    var checkedNodes = tree.getChecked('add'); // 'add' 是树组件的id
                    collectIds(checkedNodes); // 收集选中节点的id

                    console.log(menuId); // 打印所有选中节点的id

                    $('#dynamicSelect').val(menuId); // 将选中节点的id以逗号分隔的形式赋值给隐藏域

                }

            });

            layui.form.render(); // 重新渲染表单，确保Layui的样式和功能被正确应用
        },
        error: function (xhr, status, error) {
            console.error('AJAX请求失败:', status, error);
        }
    });





    window.Enroll = function Enroll() {
        //debugger;
        //菜单
        var menuId = $('#dynamicSelect').val();


        console.log(menuId)
        var roleId = $('#dynamicSelectRole').val();
        var model = JSON.stringify({ menuId: menuId, roleId: new Number(roleId) });
        console.log(model);
        var token = localStorage.getItem("token");
        $.ajax({
            url: "../User/MenuRoleAddDo",
            type: "post",
            data: { model: model, token: token },
            success: function (res) {
                console.log(res);
                debugger;
                var ressult = JSON.parse(res)
                if (ressult.code == 200) {
                    alert(ressult.message)
                    window.location.href = "../User/MenuRole";
                }
                else {
                    alert(ressult.message)
                }
            }
        })



    }

});